package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.VoiceApi;
import com.tanhua.dubbo.server.pojo.Voice;

import com.tanhua.server.vo.VoiceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Service
@Slf4j
public class VoiceService {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Autowired
    private UserInfoService userInfoService;

    @Reference(version = "1.0.0")
    private VoiceApi voiceApi;

    /**
     * 发送语音
     * @param soundFile
     * @return
     */
    public Boolean saveVoice(MultipartFile soundFile) {
        User user = UserThreadLocal.get();
        Long userId = user.getId();
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(userId);
        String gender = userInfo.getSex().name().toLowerCase();
        //System.out.println(gender);

        Voice voice = new Voice();
        voice.setUserId(user.getId());
        voice.setGender(gender);

        try {
            //上传音频
            StorePath storePath = storageClient.uploadFile(soundFile.getInputStream(),
                    soundFile.getSize(),
                    StrUtil.subAfter(soundFile.getOriginalFilename(), '.', true),
                    null);

            //设置音频url
            voice.setVoiceUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());

            String voiceId = voiceApi.saveVoice(voice);
            return StrUtil.isNotEmpty(voiceId);
        } catch (Exception e) {
            log.error("发布音频失败！file = " + soundFile.getOriginalFilename() , e);
        }
        return false;
    }

    /**
     * 接收语音
     * @return
     */
    public VoiceVo getVoice() {
        //首先获取到当前操作的用户对象
        User user = UserThreadLocal.get();
        VoiceVo voiceVo = new VoiceVo();
        Voice voice = voiceApi.queryVoiceById(user.getId());

        if (ObjectUtil.isEmpty(voice)) {
            return null;
        }
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(voice.getUserId());
        Integer remainingTimes = voiceApi.getRemainingTimes(user.getId());

        voiceVo.setSoundUrl(voice.getVoiceUrl());
        voiceVo.setUserId(userInfo.getUserId());
        voiceVo.setNickname(userInfo.getNickName());
        voiceVo.setAge(userInfo.getAge());
        voiceVo.setAvatar(userInfo.getLogo());
        voiceVo.setGender(userInfo.getSex().getValue() == 1 ? "men" : "women");
        voiceVo.setRemainingTimes(remainingTimes);

        return voiceVo;
    }
}
